package com.websocket.demo;

import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class Calculation<T> {

    private final PriorityQueue<T> sortedNoiseValueQueue;

    private final LinkedList<T> deleteToQueue = new LinkedList<>();

    private final int N;

    public Calculation(int N, Comparator<T> comparator) {
        this.N = N;
        this.sortedNoiseValueQueue = new PriorityQueue<>(comparator);
    }


    public void put(T element) {

        sortedNoiseValueQueue.add(element);

        deleteToQueue.add(element);


        if (deleteToQueue.size() >= N) {
            T poll = deleteToQueue.poll();
            log.debug("将要删除：{}，删除队列：{}",poll,Arrays.toString(deleteToQueue.toArray()));
            sortedNoiseValueQueue.remove(poll);

        }

        Iterator<T> iterator = sortedNoiseValueQueue.iterator();

        while (iterator.hasNext()){
            T next = iterator.next();
            System.out.print(next+",");
        }
        System.out.println();
    }


}
